#ifdef GL_FRAGMENT_PRECISION_HIGH
	precision highp float;
#else
	precision mediump float;
#endif

#extension GL_OES_standard_derivatives : enable

varying vec4 frag_pos;
/*
vec4 pack_depth(const in float depth)
{
    const vec4 bit_shift = vec4(256.0*256.0*256.0, 256.0*256.0, 256.0, 1.0);
    const vec4 bit_mask  = vec4(0.0, 1.0/256.0, 1.0/256.0, 1.0/256.0);
	//const vec4 bit_shift = vec4(1.0, 255.0, 255.0*255.0, 255.0*255.0*255.0);
    //const vec4 bit_mask  = vec4(1.0/255.0, 1.0/255.0, 1.0/255.0, 0.0);
    vec4 res = fract(depth * bit_shift);
    res -= res.xxyz * bit_mask;
	//res -= res.yzww * bit_mask;
    return res;
}
*/
vec4 pack_depth(const in float depth)
{
    const vec4 bit_shift = vec4(128.0*128.0*128.0, 128.0*128.0, 128.0, 1.0);
    const vec4 bit_mask  = vec4(0.0, 1.0/128.0, 1.0/128.0, 1.0/128.0);
    vec4 res = fract(depth * bit_shift);
    res -= res.xxyz * bit_mask;
    return res;
}
/*
highp vec4 encode32(highp float f) {
    highp float e =5.0;

    highp float F = abs(f); 
    highp float Sign = step(0.0,-f);
    highp float Exponent = floor(log2(F)); 
    highp float Mantissa = (exp2(- Exponent) * F);
    Exponent = floor(log2(F) + 127.0) + floor(log2(Mantissa));
    highp vec4 rgba;
    rgba[0] = 128.0 * Sign  + floor(Exponent*exp2(-1.0));
    rgba[1] = 128.0 * mod(Exponent,2.0) + mod(floor(Mantissa*128.0),128.0);  
    rgba[2] = floor(mod(floor(Mantissa*exp2(23.0 -8.0)),exp2(8.0)));
    rgba[3] = floor(exp2(23.0)*mod(Mantissa,exp2(-15.0)));
    return rgba;
}
*/
/*
vec4 pack (float depth)
{
	const vec4 bias = vec4(1.0 / 255.0,
				1.0 / 255.0,
				1.0 / 255.0,
				0.0);

	float r = depth;
	float g = fract(r * 255.0);
	float b = fract(g * 255.0);
	float a = fract(b * 255.0);
	vec4 colour = vec4(r, g, b, a);
	
	return colour - (colour.yzww * bias);
}
*/

void main(){

	//gl_FragColor = pack_depth(gl_FragCoord[2]);
	
	float depth = frag_pos[2]/frag_pos[3];
	depth = depth * 0.5 + 0.5;

	gl_FragColor = vec4(depth, 0.0, 0.0, 1.0);
	
	//gl_FragColor = vec4(gl_FragCoord[2], gl_FragCoord[2], gl_FragCoord[2], 1.0);
	
	//gl_FragColor = encode32(gl_FragCoord[2]);
	//gl_FragColor = pack_depth(frag_pos);

}